package com.taobao.modules.configServer_ex;


import java.util.List;

import com.taobao.config.client.Publisher;
import com.taobao.config.client.PublisherRegistrar;
import com.taobao.config.client.PublisherRegistration;
import com.taobao.config.client.Subscriber;
import com.taobao.config.client.SubscriberDataObserver;
import com.taobao.config.client.SubscriberRegistrar;
import com.taobao.config.client.SubscriberRegistration;
import com.taobao.config.client.Subscription;


/**
 * ConfigServer test Demo
 */
public class ConfigClientDemo {

    static String dataId = "com.playboy.magzine";

    public void publish() {
        String publisherName = "Playboy (HK) LTD.";
        PublisherRegistration<String> registration
                = new PublisherRegistration<String>(publisherName, dataId);
        Publisher<String> publisher = PublisherRegistrar.register(registration);
        publisher.publish("2008 Supplement");   // Do publish now
    }

    public void subscribe() {
        String subscriberName = "Mr. Wish";
        SubscriberRegistration registration = new SubscriberRegistration(subscriberName, dataId);
        Subscriber subscriber = SubscriberRegistrar.register(registration); // Do register
        subscriber.setDataObserver(new NewIssueNotifier());
    }

    public void subscribeAsFuture() {
        String subscriberName = "Mr. Yew";
        SubscriberRegistration registration = new SubscriberRegistration(subscriberName, dataId);
        Subscriber subscriber = SubscriberRegistrar.register(registration);
        Subscription subscription = subscriber.getSubscription();   // Get a subscription (future style)
        List<Object> issues;
        if (subscription.hasNext()) {
            try {
                issues = subscription.waitNext();                   // Publication will be returned immediately since
                                                                    //   last invocation of hasNext() returned true.
                System.out.println("Issues were already there: " + issues);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            System.out.println("No issues yet, waiting for incoming issues...");
        }

        for (;;) {
            try {
                issues = subscription.waitNext();                   // Block until next publication arrives.
            } catch (Exception e) {
                e.printStackTrace();
                break;
            }
            System.out.println("I get new issue(s): " + issues);
        }
    }

    public static void main(String[] args){
    	ConfigClientDemo demo = new ConfigClientDemo();
    	demo.publish();
//    	demo.subscribeAsFuture();
    }
}

class NewIssueNotifier implements SubscriberDataObserver {

    public void handleData(String dataId, List<Object> data) {
        System.out.println("New issue(s) arrived: " + data);
    }
}


